// Copyright 2012 The Glue BSF Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

goog.provide('glue.LMS')

//goog.require('glue.View')
goog.require('glue.Model')
goog.require('glue.Controller')
goog.require('glue.EventType')

goog.require('goog.array');
goog.require('goog.date');
goog.require('goog.object');
goog.require('goog.net.XmlHttp');
goog.require('goog.events');


/** 
 * Creates the Glue LMS base object.
 * 
 * @param {Object} opt_options 
 * @constructor
 * @export
 * @public
 */

glue.LMS = function(opt_options){
	var options = {controller:{}}
	
	goog.object.extend(options,opt_options);
	
	this.model_ = new glue.Model(options.headers);	
	this.controller_ = new glue.Controller(this.model_,options.controller);	
	
	goog.events.listen(this.model_,glue.EventType.REVALIDATE,goog.bind(function(event){
		
		this.controller_.getController().dispatchEvent(event);
	},this))
	
	
	this['debug'] = {
		'list':goog.bind(this.model_.list,this.model_),
		'clear':goog.bind(this.model_.clear,this.model_)		
	}
	window['lms'] = this;
}


/**
 * Facade for adding a view to the LMS.
 * 
 * @param {glue.View} view
 * 
 * @export
 * @public
 * @final
 */
glue.LMS.prototype.addView = function(view){
	if(this.controller_){
		this.controller_.addView(view);	
	}
}

/**
 * Facade for adding a widget to the LMS.
 * 
 * @param {glue.View} view
 * @param {glue.iController.Location} location
 * @param {Bool=} opt_prepend
 * 
 * @export
 * @public
 * @final
 */
glue.LMS.prototype.addWidget = function(view,location,opt_prepend){
	if(this.controller_){
		this.controller_.addWidget(view,location,opt_prepend);	
	}
}

/**
 * Facade for adding a logic to the LMS.
 * 
 * @param {Object} logic
 * 
 * @export
 * @public
 * @final
 */
glue.LMS.prototype.addLogic = function(logic){
	if(this.model_){
		this.model_.addLogic(logic);
	}
}

/**
 * Initalizes the LMS
 * 
 * @param {Function|Array<Function>} funcs Functions that take 1 parameter 
 *    the spec Controller and are fired on LMS initialization. 
 * 
 * @export
 * @public
 * @final
 */
glue.LMS.prototype.initialize = function(funcs){
	
	var cache = window['applicationCache'];
	if(cache){
		cache.addEventListener('updateready', function(e) {
			if (cache.status == cache.UPDATEREADY) {
				cache.swapCache();
				window.location.reload();
				
			}
		}, false);
		setInterval(function(){cache.update()},(Math.floor(Math.random()*30)+30)*60000)
		
	}
	if(goog.isArray(funcs)){
		goog.array.forEach(funcs,goog.bind(function(func){
			func(this.controller_.getController());
		},this))
	}else if(goog.isFunction(funcs)){
		funcs(this.controller_.getController())
	}
	this.controller_.initialize();
}

/**
 * Gets the Model Object
 * 
 * @return {glue.Model}
 * 
 * @export
 * @public
 * @final
 */
glue.LMS.prototype.getModel = function(){
	return this.model_;
}

/**
 * Gets the Controller Object
 * 
 * @return {glue.Controller}
 * 
 * @export
 * @public
 * @final
 */
glue.LMS.prototype.getController = function(){
	return this.getController_;
}

/**
 * Sets the cache validator.
 * 
 * @param {Function} func Function to use for Cache Validation
 * 
 * @export
 * @public
 * @final
 */
glue.LMS.prototype.setCacheValidation = function(func){
	this.model_.setCacheValidation(func);	
}

